package com.company.Lab1;

import java.util.Scanner;

public class Regretelor {
    public static void main(String[] args) {
        int[][] regretArr;
        Scanner scanner = new Scanner(System.in);
        int r, c;

        System.out.println("Introducerti numarul de randuri : ");
        r = scanner.nextInt();
        System.out.println("Introduceti numarul de coloane : ");
        c = scanner.nextInt();

        regretArr = new int[r][c];
        int[] smax = new int[r];
        int[] cmax = new int[c];
        int[][] substract = new int[r][c];
        int smin;

        System.out.println("Introduceti datele matrcii :");

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                System.out.print("[" + i + "][" + j + "] : ");
                regretArr[i][j] = scanner.nextInt();
            }
        }

        //        calcule
//        aflam maximumul pe coloana
        for (int i = 0; i < c; i++) {
            int max = regretArr[0][i];
            for (int j = 0; j < r; j++) {
                max = Math.max(max,regretArr[j][i]);
            }
            cmax[i] = max;
        }
//        substragerea, calcularea regretului
        for (int i = 0; i < c; i++) {
            for (int j = 0; j < r; j++) {
                substract[j][i] = cmax[i] - regretArr[j][i];
            }
        }
//        maximum din substract
        for (int i = 0; i < r; i++) {
            int max = substract[i][0];
            for (int j = 0; j < c; j++) {
                max= Math.max(max,substract[i][j]);
            }
            smax[i] = max;
        }
        smin = smax[0];
//        minimmul din maximele substractate
        for (int j : smax) {
            smin = Math.min(smin, j);
        }

        System.out.println("Valoarea regreta : " + smin);
    }
}
